package backend

import (
	"blog/models"
	"github.com/astaxie/beego/orm"
)

type MenuController struct {
	baseController
}

var (
	menuModel models.Menu
)

func (m *MenuController) Index() {
	m.display()
}

func (m *MenuController) Create() {
	id, _ := m.GetInt("id")
	menuType, _ := m.GetInt("type")
	level, _ := m.GetInt("level")
	m.getMenuList(id, menuType, level)
	m.Data["type"] = menuType
	menuTypeMap := menuModel.GetMenuTypeMap()
	m.Data["menuType"] = menuTypeMap
	model := models.NewMenu()
	model.Type = 1
	m.Data["model"] = model
	m.display()
}

func (m *MenuController) Edit() {
	id, _ := m.GetInt("id")
	menuType, _ := m.GetInt("type")
	level, _ := m.GetInt("level")
	if level == 1 {
		menuType = 1
	}
	menu, _ := models.GetById(models.MenuTable(), id)
	m.getMenuList(int(menu["Pid"].(int64)), menuType, level-1)
	menuTypeMap := menuModel.GetMenuTypeMap()
	m.Data["menuType"] = menuTypeMap
	m.Data["type"] = menuType
	m.Data["model"] = menu
	m.display()
}

//菜单下拉框
func (m *MenuController) getMenuList(id int, menuType int, level int) {
	var parentMenuList []map[string]interface{}
	parentMenu := make(map[string]interface{})
	parentMenu["id"] = 0
	parentMenu["name"] = "导航"
	parentMenu["select"] = 0
	if 0 == id {
		parentMenu["select"] = 1
	}
	parentMenuList = append(parentMenuList, parentMenu)
	menu, _, _ := models.GetAllData(models.MenuTable(), orm.NewCondition())
	for _, v := range menu {
		if v["level"] == 3 {
			continue
		}
		parentMenu := make(map[string]interface{})
		parentMenu["id"] = v["id"]
		parentMenu["name"] = v["menu_name"]
		parentMenu["select"] = 0
		if v["Id"] == id {
			parentMenu["select"] = 1
		}
		parentMenuList = append(parentMenuList, parentMenu)
	}

	m.Data["menuSelect"] = parentMenuList
}
